home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2018 January / PCgo 01-2018 CD-ROM Germany.iso / nw.pak / Unnamed File 000112.txt < prev    next >
Encoding:
Text File  |  2015-07-29  |  3.1 KB  |  106 lines

  1. // Copyright (c) 2010 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. /**
  6.  * @fileoverview This contains an implementation of the EventTarget interface
  7.  * as defined by DOM Level 2 Events.
  8.  */
  9.  
  10. /**
  11.  * @typedef {EventListener|function(!Event):*}
  12.  */
  13. var EventListenerType;
  14.  
  15. cr.define('cr', function() {
  16.  
  17.   /**
  18.    * Creates a new EventTarget. This class implements the DOM level 2
  19.    * EventTarget interface and can be used wherever those are used.
  20.    * @constructor
  21.    * @implements {EventTarget}
  22.    */
  23.   function EventTarget() {
  24.   }
  25.  
  26.   EventTarget.prototype = {
  27.     /**
  28.      * Adds an event listener to the target.
  29.      * @param {string} type The name of the event.
  30.      * @param {EventListenerType} handler The handler for the event. This is
  31.      *     called when the event is dispatched.
  32.      */
  33.     addEventListener: function(type, handler) {
  34.       if (!this.listeners_)
  35.         this.listeners_ = Object.create(null);
  36.       if (!(type in this.listeners_)) {
  37.         this.listeners_[type] = [handler];
  38.       } else {
  39.         var handlers = this.listeners_[type];
  40.         if (handlers.indexOf(handler) < 0)
  41.           handlers.push(handler);
  42.       }
  43.     },
  44.  
  45.     /**
  46.      * Removes an event listener from the target.
  47.      * @param {string} type The name of the event.
  48.      * @param {EventListenerType} handler The handler for the event.
  49.      */
  50.     removeEventListener: function(type, handler) {
  51.       if (!this.listeners_)
  52.         return;
  53.       if (type in this.listeners_) {
  54.         var handlers = this.listeners_[type];
  55.         var index = handlers.indexOf(handler);
  56.         if (index >= 0) {
  57.           // Clean up if this was the last listener.
  58.           if (handlers.length == 1)
  59.             delete this.listeners_[type];
  60.           else
  61.             handlers.splice(index, 1);
  62.         }
  63.       }
  64.     },
  65.  
  66.     /**
  67.      * Dispatches an event and calls all the listeners that are listening to
  68.      * the type of the event.
  69.      * @param {!Event} event The event to dispatch.
  70.      * @return {boolean} Whether the default action was prevented. If someone
  71.      *     calls preventDefault on the event object then this returns false.
  72.      */
  73.     dispatchEvent: function(event) {
  74.       if (!this.listeners_)
  75.         return true;
  76.  
  77.       // Since we are using DOM Event objects we need to override some of the
  78.       // properties and methods so that we can emulate this correctly.
  79.       var self = this;
  80.       event.__defineGetter__('target', function() {
  81.         return self;
  82.       });
  83.  
  84.       var type = event.type;
  85.       var prevented = 0;
  86.       if (type in this.listeners_) {
  87.         // Clone to prevent removal during dispatch
  88.         var handlers = this.listeners_[type].concat();
  89.         for (var i = 0, handler; handler = handlers[i]; i++) {
  90.           if (handler.handleEvent)
  91.             prevented |= handler.handleEvent.call(handler, event) === false;
  92.           else
  93.             prevented |= handler.call(this, event) === false;
  94.         }
  95.       }
  96.  
  97.       return !prevented && !event.defaultPrevented;
  98.     }
  99.   };
  100.  
  101.   // Export
  102.   return {
  103.     EventTarget: EventTarget
  104.   };
  105. });
  106.